package reflect;

import java.util.Scanner;

/**
 * 反射机制实例化对象
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Person p = new Person();
        System.out.println(p);


        /*
            java.util.ArrayList
            java.util.HashMap
            java.util.Date
         */
        Scanner scan = new Scanner(System.in);
        while (true){
            System.out.println("请输入一个类名：");
            String name = scan.nextLine();
            if ("exit".equals(name)){
                break;
            }
            //1.加载类对象
            Class cls = Class.forName(name);
            //2.实例化
            /*
                Class提供的newInstance()方法要求Class表示的类必须提供无参构造器
             */
            Object o = cls.newInstance(); //只能调用无参构造器
            System.out.println(o);

        }

    }
}
